package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        //调用Service层完成逻辑
        Emp loginEmp = empService.login(emp);
        if(loginEmp==null)
            return Result.error("用户名或密码有误！");

        //返回结果
        //生成令牌
        Map<String, Object> claims = new HashMap<>();//用来存储令牌要携带的数据
        claims.put("id", loginEmp.getId());
        claims.put("username", loginEmp.getUsername());
        //生成令牌
        String jwt = JwtUtils.generateJwt(claims);

        return Result.success(jwt);
    }
}
